var ws = require('ws');

var App = window.App = cc.Class({

    statics: {
        /** 网络对象 */
        net:ws,
        /** 用户基本信息 */
        U:Object,
        /** 当前场景 */
        gameView:cc.Component,
        /** 声音缓存 */
        sound:{},
        /** 下载资源地址 */
        path:"http://49qg.web-jia.com/c/",
        /** json配置数据 */
        C:{},
        /** 选择城镇id */
        cid:0,
        /** 选择英雄id */
        hid:0,
        /** 时间误差 */
        dtm:0,
        /** 资源名称 */
        resKey:["coin","wood","ore","sulfur","mercury","crystal","stone","addgod"],
        /** 资源名称文字 */
        resKeyName:{"coin":"金币","wood":"木头","ore":"矿石","sulfur":"硫磺","mercury":"水银","crystal":"水晶","stone":"宝石","addgod":"信仰值"},

        /** 初始化信息 */
        init: function(){
            App.net.connect("ws://127.0.0.1:9099");
        },

        /** 服务器连接成功 */
        onConnect:function(){
            cc.log("App onConnect");
            App.net.send([0,4]);
            let sd = [0,1,{}];
            //cc.log("mode",cc.sys.isNative,cc.sys.isBrowser,cc.sys.isMobile);
            //if(cc.sys.isBrowser){ //浏览器
                Fingerprint2.get({}, function(coms){
                    sd[2].account = Fingerprint2.x64hash128(coms.map(function (pair) { return pair.value }).join(), 31);
                    sd[2].pass = md5(sd[2].account + "hong");
                    App.net.send(sd);
                });
            //}
            //App.userData.login();
        },

        /** 接收到数据 */
        onMessage:function(msg){
            switch(msg[0]){
                case 0:{ //用户信息
                    if(msg[1] == 88){
                        if(msg[2] == 2 || msg[2] == 37){
                            return cc.director.loadScene("newhero");
                        }
                        if(App.C.code[msg[2]]) msg[2] = App.C.code[msg[2]];
                        App.showtip("<color=#C5C21F>"+msg[2]+"</color>");
                        return;
                    }else if(msg[1] == 103){ //创建城镇完成
                        App.U = msg[2];
                        return cc.director.loadScene("main");
                    }else if(msg[1] == 104){
                        App.dtm = Date.now() - msg[2];
                        //cc.log("App.dtm", App.dtm);
                    }
                    break;
                }
            }
            if(App.gameView) App.gameView.onNetData(msg);
        },

        /** 切换场景 */
        enterGame:function(gV){
            App.gameView = gV;
        },
        /** 播放声音 */
        playMusic:function(fl, ac){
            if(App.playGet(fl) == 0) return;  
            if(ac){
                if(fl == "bg") cc.audioEngine.playMusic(ac, true);
                else cc.audioEngine.playEffect(ac, false);
            }
            cc.loader.loadRes("/sound/"+fl+".mp3", cc.AudioClip, function (err, clip) {
                if(err){
                    cc.log("sounderror", fl, err);
                    return;
                }
                if(fl == "bg") cc.audioEngine.playMusic(clip, true);
                else cc.audioEngine.playEffect(clip, false);
            });
        },
        /** 获取声音设置 */
        playGet(fl,sv){
            var sk = "bg";
            if(fl != "bg") sk = "effect";
            var svol = cc.sys.localStorage.getItem("sound"+sk);
            cc.log("getset", sk, svol)
            if(svol == null) svol = 1;
            if(typeof(sv) != "undefined" && sv != svol){
                cc.sys.localStorage.setItem("sound"+sk, sv); 
                svol = sv;
                if(sk == "bg"){
                    cc.audioEngine.pauseMusic();
                }else{
                    cc.audioEngine.pauseAllEffects();
                }
            }
            return svol;
        },
        /** 显示提示消息 */
        showtip(msg){
            cc.loader.loadRes("/ui/tip", cc.Prefab, function (err, pfab) {
                if(err){
                    cc.log("Prefaberror", err);
                    return;
                }
                var nd = cc.instantiate(pfab); cc.log("tip", msg);
                let txt = nd.getChildByName("txt"), bg = nd.getChildByName("bg");
                txt.getComponent(cc.RichText).string = msg;
                bg.width = txt.width + 20;
                bg.height = txt.height + 20;
                cc.log(txt.width, txt.height);
                nd.parent = App.gameView.node;

                setTimeout(function(){nd.destroy();}, 3000);
            });
        },
        /** 加载指定图集中图片 */
        getFrame(url, nm, fn){
            cc.loader.loadRes(url, cc.SpriteAtlas, function (err, spa) {
                if(err){
                    cc.log("Prefaberror", err);
                    return;
                }
                fn(spa._spriteFrames[nm]);
            });
        }
    }
});

module.exports = App;